package com.leve.admin.service.impl;

import com.leve.admin.common.ResultUtil;
import com.leve.admin.domain.*;
import com.leve.admin.repository.GhostRepository;
import com.leve.admin.repository.MonmentRepository;
import com.leve.admin.repository.UserRepository;
import com.leve.admin.service.MomentService;
import com.leve.admin.service.SpaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class MomentServiceImpl implements MomentService {
    @Autowired
    private MonmentRepository monmentRepository;
    @Autowired
    private GhostRepository ghostRepository;
    @Autowired
    private UserRepository userRepository;
    @Resource
    private SpaceService spaceService;

    @Override
    public ResultBase moments(PageRequest request, String statusId, String name) {
        List<Post> moments = new ArrayList<>();
        try {
            moments = monmentRepository.moments(request, statusId, name);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询时偶尔会出现异常");
            moments = monmentRepository.moments(request, statusId, name);
        }
        return ResultUtil.success(0, "此刻列表", null, moments);
    }


    @Override
    public ResultBase findCommentsByPost(String id) {
        List<Comment> comments = null;
        try {
            comments = commentByPid(id);
        } catch (Exception e) {
            comments = commentByPid(id);
        }
        //TODO what?  因为本人前端技术一般.当 为空时需要返回当前此刻的id回去
        return ResultUtil.success(0, "此刻评论", id, comments);
    }

    @Override
    public ResultBase batchVotes(String pid, String voteNumber) {
        //随机获取是个客服号
        Post post = monmentRepository.momentById(pid);
        List<Ghost> list = new ArrayList();
        //TODO 查询性别 4
        list.addAll(ghostRepository.selectBySex("FEMALE"));
        //TODO 查询性别 3
        list.addAll(list = ghostRepository.selectBySex("MALE"));
        //循环点赞
        Random rendom = new Random();
        int forNumber = Integer.parseInt(voteNumber);
        for (int i = 0; i < list.size(); i++) {
            Ghost g = list.get(i);
            try {
                //调用循环点赞
                spaceService.createVote(pid, g.getId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ResultUtil.success(0, "后台批量点赞中", null, null);

    }

    @Override
    public ResultBase deletePost(String id) {
        spaceService.deletePost(id);
        return ResultUtil.success(0, "正在删除", null, null);
    }

    @Override
    public ResultBase createComment(String pid, Comment comment) {
        Comment comment1 = new Comment();
        try {
            comment1 = spaceService.createComment(pid, comment);
        } catch (Exception e) {
            log.info("此刻发布成功");
        }
        //其实已经发布成功了,但是space service 返回报错,不想改了
        return ResultUtil.success(0, "评论成功", null, comment1);
    }

    @Override
    public ResultBase createReply(String pid, String cid, Reply reply) {
        Reply reply1 = null;
        try {
            reply1 = spaceService.createReply(pid, cid, reply);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.success(0, "回复成功", null, reply1);
    }

    public static void main(String args[]) {
        //循环点赞
        Random rendom = new Random();
        for (int i = 0; i < 10; i++) {
            int i1 = rendom.nextInt(100 - 1);
            System.out.println(i1);
        }
    }

    private List<Comment> commentByPid(String id) {
        List<Comment> commentsByPost = monmentRepository.findCommentsByPost(id);
        commentsByPost.forEach(m -> {
            Ghost ghost = ghostRepository.selectOneByGost(m.getAuthor().getId());
            m.setAuthor(ghost);
            List<Reply> replies = m.getReplies();
            if (replies != null && replies.size() > 0) {
                replies.forEach(rep -> {
                    Ghost ghost2 = ghostRepository.selectOneByGost(rep.getAuthor().getId());
                    rep.setAuthor(ghost2);
                });
            }
        });
        return commentsByPost;
    }


}
